home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / awe2-0_1.lha / awe2-0.1 / Src / RCS / Statistic.cc,v < prev    next >
Text File  |  1989-05-04  |  6KB  |  323 lines

  1. head     3.3;
  2. branch   ;
  3. access   ;
  4. symbols  ;
  5. locks    grunwald:3.3; strict;
  6. comment  @@;
  7.  
  8.  
  9. 3.3
  10. date     89.05.04.14.38.01;  author grunwald;  state Exp;
  11. branches ;
  12. next     3.2;
  13.  
  14. 3.2
  15. date     89.02.20.15.37.44;  author grunwald;  state Exp;
  16. branches ;
  17. next     3.1;
  18.  
  19. 3.1
  20. date     88.12.20.13.49.20;  author grunwald;  state Exp;
  21. branches ;
  22. next     1.2;
  23.  
  24. 1.2
  25. date     88.10.30.13.03.25;  author grunwald;  state Exp;
  26. branches ;
  27. next     1.1;
  28.  
  29. 1.1
  30. date     88.09.18.16.42.30;  author grunwald;  state Exp;
  31. branches ;
  32. next     ;
  33.  
  34.  
  35. desc
  36. @@
  37.  
  38.  
  39. 3.3
  40. log
  41. @Before switching to Gnu lib version
  42. @
  43. text
  44. @// This may look like C code, but it is really -*- C++ -*-
  45. // 
  46. // Copyright (C) 1988 University of Illinois, Urbana, Illinois
  47. //
  48. // written by Dirk Grunwald (grunwald@@cs.uiuc.edu)
  49. //
  50. #include "Statistic.h"
  51. #include "assert.h"
  52. #include <math.h>
  53.  
  54. void
  55. Statistic::reset()
  56. {
  57.     n = 0; x = x2 = 0.0;
  58.     maxValue = -MAXFLOAT;
  59.     minValue = MAXFLOAT;
  60. }
  61.  
  62. void
  63. Statistic::operator+=(double value)
  64. {
  65.     n ++;
  66.     x += value;
  67.     x2 += (value * value);
  68.     if ( minValue > value) minValue = value;
  69.     if ( maxValue < value) maxValue = value;
  70. }
  71.  
  72. double
  73. Statistic::mean()
  74. {
  75.     if ( n > 0) {
  76.     return (x / n);
  77.     }
  78.     else {
  79.     return ( 0.0 );
  80.     }
  81. }
  82.  
  83. double
  84. Statistic::var()
  85. {
  86.     if ( n > 1) {
  87.     return(( x2 - ((x * x) /  n)) / ( n - 1));
  88.     }
  89.     else {
  90.     return ( 0.0 );
  91.     }
  92. }
  93.  
  94. double
  95. Statistic::stdDev()
  96. {
  97.     if ( n <= 0 || this -> var() <= 0) {
  98.     return(0);
  99.     } else {
  100.     return( (double) sqrt( var() ) );
  101.     }
  102. }
  103.  
  104. int
  105. Statistic::samples()
  106. {
  107.     return(n);
  108. }
  109.  
  110. double
  111. Statistic::min()
  112. {
  113.     return(minValue);
  114. }
  115.  
  116. double
  117. Statistic::max()
  118. {
  119.     return(maxValue);
  120. }
  121.  
  122. double
  123. Statistic::sum()
  124. {
  125.     return(x);
  126. }
  127.  
  128. // t-distribution: given p-value and degrees of freedom, return t-value
  129. // adapted from Peizer & Pratt JASA, vol63, p1416
  130.  
  131. double
  132. Statistic::tval(double p, int df) 
  133. {
  134.   double t;
  135.   int positive = p >= 0.5;
  136.   p = (positive)? 1.0 - p : p;
  137.   if (p <= 0.0 || df == 0)
  138.     t = HUGE;
  139.   else if (p == 0.5)
  140.     t = 0.0;
  141.   else if (df == 1)
  142.     t = 1.0 / tan((p + p) * 1.57079633);
  143.   else if (df == 2)
  144.     t = sqrt(1.0 / ((p + p) * (1.0 - p)) - 2.0);
  145.   else
  146.   {    
  147.     double ddf = df;
  148.     double a = sqrt(log(1.0 / (p * p)));
  149.     double aa = a * a;
  150.     a = a - ((2.515517 + (0.802853 * a) + (0.010328 * aa)) /
  151.              (1.0 + (1.432788 * a) + (0.189269 * aa) +
  152.               (0.001308 * aa * a)));
  153.     t = ddf - 0.666666667 + 1.0 / (10.0 * ddf);
  154.     t = sqrt(ddf * (exp(a * a * (ddf - 0.833333333) / (t * t)) - 1.0));
  155.   }
  156.   return (positive)? t : -t;
  157. }
  158.  
  159. double
  160. Statistic::confidence(int interval)
  161. {
  162.     int samp = samples();
  163.     double t = tval(double(100 + interval) * 0.005, samp );
  164.  
  165.     if (t == HUGE)
  166.     return t;
  167.     else
  168.     return (t * stdDev()) / sqrt(double( samp ));
  169. }
  170.  
  171. double
  172. Statistic::confidence(double p_value)
  173. {
  174.     int samp = samples();
  175.     double t = tval((1.0 + p_value) * 0.5, samp);
  176.     if (t == HUGE)
  177.     return t;
  178.     else
  179.     return (t * stdDev()) / sqrt(double( samp ));
  180. }
  181.  
  182. void
  183. Statistic::classPrintOn(ostream& st)
  184. {
  185.     st << samples() << " " << mean() << " " << stdDev() << " "
  186.     << confidence(90) << " " << confidence(95) << " " << confidence(99);
  187. }
  188. @
  189.  
  190.  
  191. 3.2
  192. log
  193. @Start using Gnu library heaps for schedulers
  194. @
  195. text
  196. @a10 51
  197. typedef struct tableFormat {
  198.     float n;
  199.     float parameter[3];
  200. };
  201.  
  202. //
  203. //    The following is Students T-distribution with parameter
  204. //    alpha     = 0.95  (for 90%)
  205. //        = 0.975 (for 95%)
  206. //        = 0.995 (for 99%)
  207. //
  208. static const int tableEntries = 35;
  209.  
  210. static const tableFormat StudentT[ tableEntries ] = {
  211. {       1, { 6.314, 12.706, 63.657}},
  212. {       2, { 2.920,  4.303,  9.925}},
  213. {       3, { 2.353,  3.182,  5.841}},
  214. {       4, { 2.132,  2.776,  4.604}},
  215. {       5, { 2.015,  2.571,  4.032}},
  216. {       6, { 1.943,  2.447,  3.707}},
  217. {       7, { 1.895,  2.365,  3.499}},
  218. {       8, { 1.860,  2.306,  3.355}},
  219. {       9, { 1.833,  2.262,  3.250}},
  220. {      10, { 1.812,  2.228,  3.169}},
  221. {      11, { 1.796,  2.201,  3.106}},
  222. {      12, { 1.782,  2.179,  3.055}},
  223. {      13, { 1.771,  2.160,  3.012}},
  224. {      14, { 1.761,  2.145,  2.977}},
  225. {      15, { 1.753,  2.131,  2.947}},
  226. {      16, { 1.746,  2.210,  2.921}},
  227. {      17, { 1.740,  2.110,  2.898}},
  228. {      18, { 1.734,  2.101,  2.878}},
  229. {      19, { 1.729,  2.093,  2.861}},
  230. {      20, { 1.725,  2.086,  2.845}},
  231. {      21, { 1.721,  2.080,  2.831}},
  232. {      22, { 1.717,  2.074,  2.819}},
  233. {      23, { 1.714,  2.069,  2.807}},
  234. {      24, { 1.711,  2.064,  2.797}},
  235. {      25, { 1.708,  2.060,  2.787}},
  236. {      26, { 1.706,  2.056,  2.779}},
  237. {      27, { 1.703,  2.052,  2.771}},
  238. {      28, { 1.701,  2.048,  2.763}},
  239. {      29, { 1.699,  2.045,  2.756}},
  240. {      30, { 1.697,  2.042,  2.750}},
  241. {      40, { 1.684,  2.021,  2.704}},
  242. {      50, { 1.676,  2.009,  2.678}},
  243. {      75, { 1.665,  1.992,  2.643}},
  244. {     100, { 1.660,  1.984,  2.626}},
  245. {MAXFLOAT, { 1.645,  1.960,  2.576}}
  246. };
  247.  
  248. d22 1
  249. a22 1
  250.     n += 1;
  251. d61 6
  252. d68 1
  253. a68 1
  254. Statistic::confidence(int interval)
  255. d70 2
  256. a71 1
  257.     int index;
  258. d73 5
  259. a77 14
  260.     switch( interval ) {
  261.     case 90 : 
  262.     index = 0;
  263.     break;
  264.     case 95:
  265.     index = 1;
  266.     break;
  267.     case 99:
  268.     index = 2;
  269.     break;
  270.     default :
  271.     index = -1;
  272.     break;
  273.     }
  274. d79 5
  275. a83 1
  276.     assert(index >= 0 && index <= 2);
  277. d85 2
  278. a86 3
  279.     if (n < 1) {
  280.     return(0);
  281.     }
  282. d88 27
  283. a114 2
  284.     double dn = double(n);
  285.     for (int i = 0; dn > StudentT[i].n; i++);
  286. d116 5
  287. a120 2
  288.     double meanValue = mean();
  289.     double sd = stdDev();
  290. d122 5
  291. a126 2
  292.     assert( i >= 0 && i < tableEntries);
  293.     double zAlpha = StudentT[i].parameter[index];
  294. d128 9
  295. a136 1
  296.     return( (zAlpha * sd) / sqrt( dn ) );
  297. @
  298.  
  299.  
  300. 3.1
  301. log
  302. @Steay version
  303. @
  304. text
  305. @@
  306.  
  307.  
  308. 1.2
  309. log
  310. @*** empty log message ***
  311. @
  312. text
  313. @@
  314.  
  315.  
  316. 1.1
  317. log
  318. @Initial revision
  319. @
  320. text
  321. @d1 6
  322. @
  323.